<?php

namespace App\Http\Controllers;



use App\Models\Area;
use App\Models\Car;
use App\Models\Complaint;
use App\Models\Credit;
use App\Models\Cts;
use App\Models\CtsProduct;
use App\Models\Guide;
use App\Models\Hotel;
use App\Models\Notice;
use App\Models\Restaurant;
use App\Models\Rural;
use App\Models\Scene;
use App\Models\Team;
use Carbon\Carbon;
use DB;
use Encore\Admin\Form\Field\Date;
use Illuminate\Support\Facades\Input;

class SincerityController extends AuthController
{

    public function index(){
        $irregularities = Complaint::select(['behavior',DB::raw('Count(behavior) as count')])->groupBy('behavior')->get();
        foreach ($irregularities as $val){
            $val->behavior = config('options.complaint_behavior')[$val->behavior];
        }
        return response()->json([
            'team'=>[
                'count'=>Team::getTodayTeamCount(),
                'person_num'=>Team::getTodayPersonCount()
            ],
            'merchants'=>[
                ['name'=>'景区','value'=>Scene::count()],
                ['name'=>'乡村旅游','value'=>Rural::count()],
                ['name'=>'旅行社','value'=>Cts::count()],
                ['name'=>'导游','value'=>Guide::count()],
                ['name'=>'旅行车辆','value'=>Car::count()],
                ['name'=>'星级酒店','value'=>Hotel::count()],
                ['name'=>'旅行餐饮','value'=>Restaurant::sum('num')],
            ],
            'Irregularities'=>$irregularities
        ]);
    }

    public function complain(){
        $type = Complaint::select(['type',DB::raw('Count(type) as count')])->groupBy('type')->get();
        foreach ($type as $val){
            $val->type = config('options.complaint_type')[$val->type];
        }
        $status = Complaint::select(['status',DB::raw('Count(status) as count')])->groupBy('status')->get();
        foreach ($status as $val){
            $val->status = config('options.complaint_status')[$val->status];
        }
        return response()->json([
            'count'=>Complaint::count(),
            'type'=>$type,
            'status'=>$status
        ]);
    }

    public function rb($type=0,$take=5){
        $area_id = request()->area_id;
        $where = $area_id==0?[]:[['area_id',$area_id]];
        if($type==0){
            $list = Cts::where('level','<=',2)->where($where)->take($take)->orderBy('level')->get(['title','credit_score','credit_record','level']);
        }else{
            $list = Cts::where('level','>',2)->where($where)->take($take)->orderByDesc('credit_score')->get(['title','credit_score','credit_record','level']);
        }
        foreach ($list as $val){
            $val->level = config('options.cts_level')[$val->level];
            $val->credit_record = $val->credit_record?config('options.cts_credit_record')[$val->credit_record]:'';
        }
        return response()->json($list);
    }

    public function ctsSearch(){
        $title = request()->title;
        $list = Cts::where('title','like','%'.$title.'%')->get(['title','legal_person','level','licence','credit_record','credit_code']);
        foreach ($list as $val){
            $val->level = $val->level?config('options.cts_level')[$val->level]:'';
            $val->credit_record = $val->credit_record?config('options.cts_credit_record')[$val->credit_record]:'';
        }
        return response()->json($list);
    }

    public function creditAnalysis(){
        $data = Credit::integrityAnalysis('year','cts');
        return response()->json($data);
    }

    public function guideAnalysis($limit = 6){
        $data = Credit::integrityAnalysis('month','guides',$limit);
        return response()->json($data);
    }

    public function getAll($year=null){
        $list = Credit::getAllAnalysis($year);
        return response()->json($list);
    }

    /*
     * 诚信三级页面
     * */
    public function merchantInfo(){
        $type = request()->type?request()->type:'scenes';
        $area_id = request()->area_id;
        $search = Input::has('search')?request()->search:'';
        $types = collect(config('options.merchant'));
        if($type=='guides'||$type=='cars'){
            $where = [];
        }else{
            $where = [
                ['area_id',$area_id]
            ];
        }
        $keyword = $type=='guides'?'name':($type=='cars'?'license_plate_number':'title');
        if(Input::has('area_id')){
            $list = DB::table($type)->where($where)->where($keyword,'like','%'.$search.'%')->get();
        }else
            $list = DB::table($type)->where($keyword,'like','%'.$search.'%')->get();
        if($type=='scenes'){
            foreach ($list as $val){
                $val->level = config('options.scene_level')[$val->level];
                $val->area_id = Area::find($val->area_id)->title;
            }
        }elseif($type=='cts'){
            foreach ($list as $val){
                $val->area_id = Area::find($val->area_id)->title;
            }
        }elseif($type=='guides'){
            foreach ($list as $val){
                $val->level = config('options.guide_level')[$val->level];
                $val->languages = config('options.guides_languages')[$val->languages];
            }
        }elseif($type=='hotels'){
            foreach ($list as $val){
                $val->level = config('options.hotel_star')[$val->level];
                $val->area_id = Area::find($val->area_id)->title;
            }
        }
        return response()->json([
            'types'=>$types,
            'data'=>$list
        ]);
    }
    public function complaintInfo(){
        $types = config('options.complaint_type');
        $areas = Area::pluck('title','id');
        $status = config('options.complaint_status');

        $type=request()->type;
        $area_id=request()->area_id;
        $statu=request()->status;
        $where1 =$type==0?[]:[['type',$type]];
        $where2 =$area_id==0?[]:[['area_id',$area_id]];
        $where3 =[['status',$statu]];

        $list = Complaint::select(['target','target_id','area_id','status','other','behavior','cause'])->where($where1)->where($where2)->where($where3)->get()->map(function ($val){
            //return $val;
            $area = Area::find($val->area_id);
            $object = DB::table($val->target)->find($val->target_id);
            return [
                'target'=>config('options.credit_module')[$val->target],
                'object'=>$object?$object->title:'',
                'area'=>$area?$area->title:'',
                'behavior'=>config('options.complaint_behavior')[$val->behavior],
                'status'=>config('options.complaint_status')[$val->status],
                'info'=>$val->other,
                'cause'=>$val->cause
            ];
        });

        return response()->json([
            'types'=>$types,
            'areas'=>$areas,
            'status'=>$status,
            'data'=>$list
        ]);
    }

    public function notice(){
        $area_id = request()->area_id;
        $where = $area_id==0?[]:[['area_id',$area_id]];
        $notice = Notice::select(['updated_at','content'])->where([['display',0],['type',1]])->orderByDesc('updated_at')->first();
        $permit =Cts::select(['licence','business','title','legal_person','level'])->where($where)->get();

        foreach ($permit as $val){
            $val->level = config('options.cts_level')[$val->level];
        }
        $punish = Complaint::select(['enforcement_number','name','handle_person','behavior','target','target_id','other','handle_time','cause','result'])->where('status',2)->where($where)->get();
        foreach ($punish as $val){
            if($val->target=='guides')
                $text = $val->target_id?DB::table($val->target)->find($val->target_id)->name:'';
            else
                $text = $val->target_id?DB::table($val->target)->find($val->target_id)->title:'';
            $val->target = $val->target?config('options.credit_module')[$val->target]:'';
            $val->target_id = $text;
            $val->behavior=config('options.complaint_behavior')[$val->behavior];
        }
        return response()->json([
            'new_notice'=>$notice,
            'punish'=>$punish,
            'permit'=>$permit
        ]);
    }

    public function negativeCtsTop10(){
        $area_id = request()->area_id;
        $where = $area_id==0?[]:[['area_id',$area_id]];
        $list = Cts::where('level','>',2)->where($where)->take(10)->orderByDesc('credit_score')->get(['title','credit_score','credit_record','level']);
        foreach ($list as $val){
            $val->level = config('options.cts_level')[$val->level];
            $val->credit_record = $val->credit_record?config('options.cts_credit_record')[$val->credit_record]:'';
        }
        return response()->json($list);
    }

    public function negativeCtsProduct(){
        $list = CtsProduct::where('type',1)->get();
        return response()->json($list);
    }

}
